package com.yanqu.road.dao.impl.activity.flowercard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.flowercards.FlowerCard;
import com.yanqu.road.entity.activity.flowercards.GameStatus;
import com.yanqu.road.entity.activity.flowercards.UserFlowerCardsData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserFlowerCardsDataDaoImpl extends TempDataObjectDao<UserFlowerCardsData> {

    public static String getUserFlowerCardsDataSql = "select * from `t_u_flower_cards_data` where `activityId`=? and `userId`=?";
    public UserFlowerCardsData getUserFlowerCardsData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserFlowerCardsDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserFlowerCardsDataSql = "update `t_u_flower_cards_data` set freeTimes=?,cardList=?,gameStatus=?,maxAffinity=?," +
            "totalAffinity=?,maxAppraise=?,buySliverToken=?,buyGoldToken=?,resetTime=?,totalTimes=?,alwaysShow=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserFlowerCardsData userFlowerCardsData) {
        userFlowerCardsData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFlowerCardsData.getCardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFlowerCardsData.getGameStatus())));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getMaxAffinity()));
        para.put(i++, new DbParameter(Types.BIGINT, userFlowerCardsData.getTotalAffinity()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getMaxAppraise()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getAlwaysShow()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFlowerCardsData.getUserId()));
        if (getDb().executeNoneQuery(updateUserFlowerCardsDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserFlowerCardsDataSql = "insert into `t_u_flower_cards_data`(activityId,userId,freeTimes,cardList,gameStatus,maxAffinity," +
            "totalAffinity,maxAppraise,buySliverToken,buyGoldToken,resetTime,totalTimes,alwaysShow) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserFlowerCardsData userFlowerCardsData) {
        userFlowerCardsData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFlowerCardsData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFlowerCardsData.getCardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFlowerCardsData.getGameStatus())));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getMaxAffinity()));
        para.put(i++, new DbParameter(Types.BIGINT, userFlowerCardsData.getTotalAffinity()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getMaxAppraise()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFlowerCardsData.getAlwaysShow()));
        if (getDb().executeNoneQuery(insertUserFlowerCardsDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserFlowerCardsData parseData(ResultSet rs) throws SQLException {
        UserFlowerCardsData data = new UserFlowerCardsData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setFreeTimes(rs.getInt("freeTimes"));
        data.setCardList(JSON.parseObject(rs.getString("cardList"), new TypeReference<List<FlowerCard>>() {}));
        data.setGameStatus(JSON.parseObject(rs.getString("gameStatus"), GameStatus.class));
        data.setMaxAffinity(rs.getInt("maxAffinity"));
        data.setTotalAffinity(rs.getLong("totalAffinity"));
        data.setMaxAppraise(rs.getInt("maxAppraise"));
        data.setBuySliverToken(rs.getInt("buySliverToken"));
        data.setBuyGoldToken(rs.getInt("buyGoldToken"));
        data.setResetTime(rs.getInt("resetTime"));
        data.setTotalTimes(rs.getInt("totalTimes"));
        data.setAlwaysShow(rs.getInt("alwaysShow"));
        return data;
    }
}
